import requests
import pandas as pd
import time

url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=331570116174'
os_url = 'https://c.m.163.com/ug/api/wuhan/app/data/oversea-total?t=165805417706'

headers = {
# 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49'
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0'
}

response = requests.get(url,headers=headers)
os_response = requests.get(url=os_url,headers=headers)

def save_data(data,name):
    file_name = name+'_'+time.strftime('%Y_%m_%d',time.localtime(time.time()))+'.csv'
    data.to_csv(file_name,index=None,encoding='utf_8_sig')
    print(file_name+'保存成功')


def get_data(data,table_name,*args):
    heads = pd.DataFrame(data)[list(args)]
    today = pd.DataFrame([i['today'] for i in data])
    total = pd.DataFrame([i['total'] for i in data])
    today.columns = [f"today_{i}" for i in today.columns]
    total.columns = [f"total_{i}" for i in total.columns]
    table_name = pd.concat([heads, today, total],axis = 1)
    return table_name

def history_data(name,u_name,table_name):
    name_id = name[['name', 'id']].values.tolist()
    urls = [f"https://c.m.163.com/ug/api/wuhan/app/data/list-by-area-code?areaCode={i[1]}&" for i in name_id]
    for index, item in enumerate(urls):
        response = requests.get(item, headers=headers)
        data = response.json()['data']['list']
        concat_data = get_data(data, 'concat_data', 'date')
        concat_data.insert(1, u_name, name_id[index][0])
        if index == 0:
            table_name = concat_data
        else:
            table_name = pd.concat([table_name, concat_data], axis=0)

        print(f"{name_id[index][0]}数据抓取完成,共获取到{concat_data.shape[0]}条数据")
    return table_name

#中国实时数据
china_total_now =pd.DataFrame(response.json()['data']['chinaTotal'])
china_total_now['lastUpdateTime'] = response.json()['data']['lastUpdateTime']
save_data(china_total_now,'china_total_now')

#世界实时数据
world_total_now = pd.DataFrame(os_response.json()['data'])
save_data(world_total_now,'world_total_now')

#省实时数据
prov_d = response.json()['data']['areaTree'][2]['children']
china_all_provinces_data_now = get_data(prov_d,'china_all_provinces_data_now',*['id','name','lastUpdateTime'])
save_data(china_all_provinces_data_now,'china_all_provinces_data_now')

#世界数据
country_d = response.json()['data']['areaTree']
world_all_country_data_now = get_data(country_d,'world_all_country_data_now',*['id','name','lastUpdateTime'])
save_data(world_all_country_data_now,'world_all_country_data_now')


#市实时数据
for z,j in enumerate(response.json()['data']['areaTree'][2]['children']):
    head = pd.DataFrame(j['children'])[['id','name','lastUpdateTime']]
    head['provinces'] = response.json()['data']['areaTree'][2]['children'][z]['name']
    today = pd.DataFrame([i['today'] for i in j['children']])
    total = pd.DataFrame([i['total'] for i in j['children']])
    today.columns = [f"today_{i}" for i in today.columns]
    total.columns = [f"total_{i}" for i in total.columns]
    cat_table = pd.concat([head, today, total],axis = 1)
    if z == 0:
        all_cities_data = cat_table
    else:
        all_cities_data = pd.concat([all_cities_data,cat_table],axis =0 )
save_data(all_cities_data,'all_cities_data')

#中国历史数据
china_history_data = response.json()['data']['chinaDayList']
china_history_datas = get_data(china_history_data,'china_history_datas',*['date'])
save_data(china_history_datas,'china_history_datas')


#中国各省历史数据
china_all_provinces_history_data = history_data(china_all_provinces_data_now,'province_name','china_all_provinces_history_data')
save_data(china_all_provinces_history_data,'china_all_provinces_history_data')


#世界各国历史数据
world_all_country_history_data = history_data(world_all_country_data_now,'country_name','world_all_country_history_data')
save_data(world_all_country_history_data,'world_all_country_history_data')

















